package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer
import ModelLayer.*;



public class AddressBookUI
{   
    private AddressCtr addressCtr;
    Person person;
    
    
    public AddressBookUI()
    {
      addressCtr = new AddressCtr();
    }

   public void addPerson(long id, String name, String address, String postalCode, String city, String phone)
   {
    addressCtr.createPerson( id, name, address, postalCode, city, phone);
   }
   public void deletePerson(long id)
   {
       addressCtr.deletePerson(id);
   }
   public void updatePerson(long id, String name, String address)
   {
       addressCtr.updatePerson(id, name, address);
    }
   public void listAllPersons()
    {
        addressCtr.listAllPersons();
    }
    public void start()
    {
       
        addressMenu();
    }
    
    public void addressMenu()
    {
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
          int choice = writeAddressBookMenu(); 
          if(choice == 1)
          {   //show person
              showPersonInfo();
          }
          else{
              if(choice == 2){
                    //create person
                    createPerson();
              }  
              else{
                  if(choice == 3){
                      //delete person
                      deletePerson();
                  }  
                  else{
                      if(choice == 4){
                          // update person
                          updatePerson();
                      }
                      else{
                          if(choice == 5){
                              // listAllPersons();
                              listAllPersons();
                              waitForInput();
                            }                      
                            else
                           {
                               if(choice == 6)
                               {
                                   addCopyToPerson();
                                }
                            
                           else
                           {
                               if(choice == 6)
                               {
                                   addCopyToPerson();
                                }
                                else
                                {
                                    if(choice == 7)
                                    {
                                        deleteCopy();
                                    }
                                
                            
                                   
                               
                          else{
                              exit = true;
                           }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
           
       }//end while
    }}}}
    public int writeAddressBookMenu()
    {      // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** AddressBook Menu ***");
            System.out.println(" (1) Show information about person");
            System.out.println(" (2) Add person");
            System.out.println(" (3) Delete person");
            System.out.println(" (4) Update person");
            System.out.println(" (5) List all persons");
            System.out.println(" (6) Add copy to the person");
            System.out.println(" (7) remove copy from a person");
            System.out.println(" (8) return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
        }
        
    //the method reads id for a person and return
    // 
    public long inputPersonsID()
    {   
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Id:  ");
           try{
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
    
    public String inputName()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Name:  ");
         String name = keyboard.nextLine();
         return name;
    }
   
    public String inputPostalCode()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Postal Code:  ");
         String code = keyboard.nextLine();
         return code;
    }
    public String inputCity()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write City:  ");
         String city = keyboard.nextLine();
         return city;
    } 
    public String inputPhone()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Phone Number:  ");
         String phone = keyboard.nextLine();
         return phone;
    } 
     
    public String  inputAddress()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Address: ");
        String address = keyboard.nextLine();
        return address;
    }
    
    public void showId(long id)
    {
        System.out.println("ID of the person: " + id);
    }
    public void showName(String name)
    { 
        System.out.println("The name of the persone: " + name);
    }
    public void showAddress(String address)
    { 
        System.out.println("The address of the person: " + address);
    }

    private void getPerson()
    {//is to be changed when the address book controller is completed
        long id = inputPersonsID();
      }   
    
    public Person findPerson(long id)
    {
        Person person = addressCtr.getPerson(id);
        if(person != null)
        {
            return person;
        }
        else
        {
            return null;
        }
    }
    private void createPerson()
    {
        long id = inputPersonsID();
        String name = inputName();
        String address = inputAddress();
        String postalCode = inputPostalCode();
        String city = inputCity();
        String phone = inputPhone();
        addressCtr.createPerson( id, name, address, postalCode, city, phone);
        waitForInput();
    }
    private void showPersonInfo()
    {
       long id = inputPersonsID();
       Person person = addressCtr.getPerson(id);
       if(addressCtr.getPerson(id) != null)
       {
       showName(person.getName());
       showAddress(person.getAddress());
       waitForInput();
    }
    else
    {
        System.out.println("There is no person with that id");
        waitForInput();
    }
    }
    private void deletePerson()
    {
       long id = inputPersonsID();
       Person person = (addressCtr.getPerson(id));
       if(addressCtr.getPerson(id) != null)
       {
       addressCtr.deletePerson(id);
       System.out.println("The person has been deleted");
       waitForInput();
    }
    else
    {
        System.out.println("There is no person with that id");
       waitForInput();
    }
}
    private void updatePerson()
    {
        long id = inputPersonsID();
               Person person = (addressCtr.getPerson(id));
       if(addressCtr.getPerson(id) != null)
       {
        String name = inputName();
        String address = inputAddress();
        addressCtr.updatePerson(id, name, address);
    }
    else
    {
        System.out.println("There is no person with that id");
        waitForInput();
    }
}
    private void waitForInput()
    {
        System.out.println("Press Enter to continue");
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    public void addCopyToPerson()
    {
        String date = inputCopyPurchaseDate();
        long idPerson = inputPersonsID();
        Person person = addressCtr.getPerson(idPerson);
        double price = inputCopyPrice();
        int serialNumber = inputCopyNo();
        person.addCopyToThePerson(serialNumber, date, price);
}
private int inputDVDNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD number: ");
        int no = keyboard.nextInt();
        return no;
    }
        private int inputCopyNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the copy serial number: ");
        int no = keyboard.nextInt();
        return no;
    }
        private double inputCopyPrice()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type copy price: ");
        double copyPrice = keyboard.nextInt();
        return copyPrice;
    }
    
        private String inputCopyPurchaseDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type copy purchase date: ");
        String purchaseDate = keyboard.nextLine();
        return purchaseDate;
    }
    public void  deleteCopy()
{
     long idPerson = inputPersonsID();
        Person person = addressCtr.getPerson(idPerson);
    int serialNumber = inputCopyNo();
    person.removeCopy(serialNumber);
}

}